package com.hmall.controller;

import com.hdmall.business.common.annotation.LoginRequired;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.model.MallTransport;
import com.hmall.model.SysUser;
import com.hmall.service.TransportService;
import com.hmall.util.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * @ClassName TransportController
 * @Description //
 * @Author zhoutao
 * @Date 2018/10/23 10:45
 * @Version 2.0
 **/
@RestController
@Api(value = "自提地址管理", description = "自提地址管理API")
@RequestMapping("/transport/")
public class TransportController {

    @Autowired
    TransportService transportService;

    @Autowired
    JedisPool jedisPool;

    TokenUtil tokenUtil = new TokenUtil();


    /***
     * @Author zhoutao
     * @Description // 用户新增运输方式
     * @Date 2018/10/23 11:07
     * @Param [token, linkmanName, linkmanTel, plateNumber, license, defaultCheck]
     * @return com.hmall.common.ServerResponse<java.util.List<com.hmall.model.MallTransport>>
     **/
    @LoginRequired
    @PostMapping("createTransport")
    @ApiOperation(value = "新增运输方式", notes = "支持后台")
    public ServerResponse<List<MallTransport>> createTransport(String token, Integer customerUserId, String linkmanName, String linkmanTel, String plateNumber, String license, Integer defaultCheck, String remark) throws Exception {

        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return transportService.createTransport(getUserId(user, customerUserId), linkmanName, linkmanTel, plateNumber, license, defaultCheck, remark);
    }


    /**
     * @Author zhoutao
     * @Description // 删除运输方式
     * @Date 2018/10/23 11:14
     * @Param [token, transportId]
     * @return com.hmall.common.ServerResponse
     **/
    @LoginRequired
    @GetMapping("deleteTransport")
    @ApiOperation(value = "删除运输方式")
    public ServerResponse deleteTransport(String token, Integer transportId) {

        return transportService.deleteTransport(transportId);
    }


    /**
     * @Author zhoutao
     * @Description // 用户修改运输方式
     * @Date 2018/10/23 11:16
     * @Param [token, transportId, linkmanName, linkmanTel, plateNumber, license, defaultCheck, remark]
     * @return com.hmall.common.ServerResponse
     **/
    @LoginRequired
    @PostMapping("updateTransport")
    @ApiOperation(value = "修改运输方式", notes = "支持后台")
    public ServerResponse updateTransport(String token, Integer customerUserId, Integer transportId, String linkmanName, String linkmanTel, String plateNumber, String license, Integer defaultCheck, String remark) throws Exception {

        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return transportService.updateTransport(getUserId(user, customerUserId), transportId, linkmanName, linkmanTel, plateNumber, license, defaultCheck, remark);
    }


    /***
     * @Author zhoutao
     * @Description // 得到预订单界面自提的所有的运输方式
     * @Date 2018/10/23 10:51
     * @Param [token]
     * @return com.hmall.common.ServerResponse
     **/
    @LoginRequired
    @GetMapping("getTransportList")
    @ApiOperation(value = "得到运输方式列表", notes = "支持后台")
    public ServerResponse getTransportList(String token, Integer customerUserId) throws Exception {

        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return transportService.getTransportList(getUserId(user, customerUserId));
    }

    /**
     * @Author zhoutao
     * @Description // 用户设置默认地址
     * @Date 2018/10/23 11:30
     * @Param [token, defaultCheck, transportId]
     * @return com.hmall.common.ServerResponse
     **/
    @LoginRequired
    @GetMapping("setDefaultTransport")
    @ApiOperation(value = "设置默认地址或者取消", notes = "支持后台")
    public ServerResponse setDefaultTransport(String token, Integer customerUserId, Integer defaultCheck, Integer transportId) throws Exception {

        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return transportService.setDefaultTransport(getUserId(user, customerUserId), defaultCheck, transportId);
    }

    private Integer getUserId(SysUser user, Integer customerUserId){

        Integer userId = user.getUserId();
        if(null != customerUserId && "".equals(customerUserId) == false){
            userId = customerUserId;
        }
        return userId;
    }
}
